using System;
using NodeCanvas.Variables;

namespace NodeCanvas.Actions
{
	[Category("✫ Utility")]
	[Description("Alternative use to set boolean, int and float blackboard variables")]
	public class ExecuteExpression : ActionTask
	{
		public string expression;

		private string leftVar;

		private string operation;

		private string rightVar;

		private Type type;

		private object leftValue;

		private object rightValue;

		private string error;

		protected override string info
		{
			get
			{
				return (!string.IsNullOrEmpty(error)) ? error : ("'" + expression + "'");
			}
		}

		protected override void OnExecute()
		{
			string[] array = expression.Split(' ');
			if (array.Length != 3 || string.IsNullOrEmpty(array[2]))
			{
				Error("Wrong format");
				return;
			}
			leftVar = array[0];
			operation = array[1];
			rightVar = array[2];
			rightValue = null;
			VariableData data = base.blackboard.GetData(rightVar, type);
			if (data != null)
			{
				rightValue = data.objectValue;
			}
			leftValue = base.blackboard.GetDataValue<object>(leftVar);
			if (leftValue == null)
			{
				Error("No variable exists");
				return;
			}
			type = leftValue.GetType();
			if (type != typeof(bool) && type != typeof(float) && type != typeof(int))
			{
				Error("Unsupported Variable Type");
				return;
			}
			error = null;
			try
			{
				if (type == typeof(bool))
				{
					SetBool();
				}
				if (type == typeof(float))
				{
					SetFloat();
				}
				if (type == typeof(int))
				{
					SetInt();
				}
			}
			catch
			{
				Error("Parsing Error");
				return;
			}
			EndAction();
		}

		private void Error(string err)
		{
			error = "<color=#d63e3e>" + err + "</color>";
			EndAction(false);
		}

		private void SetBool()
		{
			if (rightValue == null)
			{
				rightValue = bool.Parse(rightVar);
			}
			if (operation == "=")
			{
				base.blackboard.SetDataValue(leftVar, (bool)rightValue);
			}
			else if (operation == "!=")
			{
				base.blackboard.SetDataValue(leftVar, !(bool)rightValue);
			}
			else
			{
				Error("Wrong Format");
			}
		}

		private void SetFloat()
		{
			if (rightValue == null)
			{
				rightValue = float.Parse(rightVar);
			}
			if (operation == "=")
			{
				base.blackboard.SetDataValue(leftVar, rightValue);
			}
			else if (operation == "+=")
			{
				base.blackboard.SetDataValue(leftVar, base.blackboard.GetDataValue<float>(leftVar) + (float)rightValue);
			}
			else if (operation == "-=")
			{
				base.blackboard.SetDataValue(leftVar, base.blackboard.GetDataValue<float>(leftVar) - (float)rightValue);
			}
			else if (operation == "*=")
			{
				base.blackboard.SetDataValue(leftVar, base.blackboard.GetDataValue<float>(leftVar) * (float)rightValue);
			}
			else if (operation == "/=")
			{
				base.blackboard.SetDataValue(leftVar, base.blackboard.GetDataValue<float>(leftVar) / (float)rightValue);
			}
			else
			{
				Error("Wrong Format");
			}
		}

		private void SetInt()
		{
			if (rightValue == null)
			{
				rightValue = int.Parse(rightVar);
			}
			if (operation == "=")
			{
				base.blackboard.SetDataValue(leftVar, rightValue);
			}
			else if (operation == "+=")
			{
				base.blackboard.SetDataValue(leftVar, base.blackboard.GetDataValue<int>(leftVar) + (int)rightValue);
			}
			else if (operation == "-=")
			{
				base.blackboard.SetDataValue(leftVar, base.blackboard.GetDataValue<int>(leftVar) - (int)rightValue);
			}
			else if (operation == "*=")
			{
				base.blackboard.SetDataValue(leftVar, base.blackboard.GetDataValue<int>(leftVar) * (int)rightValue);
			}
			else if (operation == "/=")
			{
				base.blackboard.SetDataValue(leftVar, base.blackboard.GetDataValue<int>(leftVar) / (int)rightValue);
			}
			else
			{
				Error("Wrong Format");
			}
		}
	}
}
